package com.xialing.controller;


import com.xialing.controller.request.LinkGroupAddRequest;
import com.xialing.controller.request.LinkGroupUpdateRequest;
import com.xialing.enums.BizCodeEnum;
import com.xialing.service.LinkGroupService;
import com.xialing.util.JsonData;
import com.xialing.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 短链接分组表 前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {

    @Autowired
    private LinkGroupService linkGroupService;

    /**
     * 添加分组
     * @param request
     * @return
     */
    @PostMapping("/add")
    public JsonData add(@RequestBody LinkGroupAddRequest request){
        int res = linkGroupService.add(request);
        return res == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }


    /**
     * 删除分组
     * @param groupId
     * @return
     */
    @DeleteMapping("/delete/{groupId}")
    public JsonData delete(@PathVariable Long groupId){
        int res = linkGroupService.delete(groupId);
        return res == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
    }

    /**
     * 获取分组详情
     * @param groupId
     * @return
     */
    @GetMapping("/detail/{groupId}")
    public JsonData detail(@PathVariable Long groupId){
        LinkGroupVO linkGroupVO = linkGroupService.detail(groupId);
        return JsonData.buildSuccess(linkGroupVO);
    }


    /**
     * 获取分组列表
     * @return
     */
    @GetMapping("/list")
    public JsonData findUserAllLinkGroup(){
        return JsonData.buildSuccess(linkGroupService.listAllGroup());
    }


    @PostMapping("/update")
    public JsonData update(@RequestBody LinkGroupUpdateRequest request){
        int res = linkGroupService.updateById(request);
        return res == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }

}

